# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  
  def title(page_title, html_options = {})
    content_for(:title) { page_title }
    content_tag(:h1, page_title, html_options)
  end
  
  def subtitle(page_subtitle)
    content_for(:subtitle) { page_subtitle }
  end
  
  def pluralize(count, word)
    case count
      when 0 then "#{word[:other]}"
      when 1 then "#{word[:one]}"
      else "#{word[:other]}"
    end
  end
  
  def awesome_submit_button(label, icon = "", html_options = {})
    if html_options[:class].nil?
      html_options[:class] = "awesome silver green"
    else
      html_options[:class] = "awesome #{html_options[:class]}"
    end
    
    content_tag(:button, html_options) do
      content_tag(:span, :class => "icon #{icon}") { label }
    end
  end
  
  def generate_html(form_builder, method, options = {})
    options[:object] ||= form_builder.object.class.reflect_on_association(method).klass.new
    options[:partial] ||= method.to+s.singularize
    options[:form_builder_local] ||= :form
    
    form_builder.fields_for(method, options[:object], :child_index => "NEW_RECORD") do |f|
      locals = {options[:form_builder_local] => f}
      render :partial => options[:partial], :locals => locals
    end
  end
  
  def generate_template(form_builder, method, options = {})
    escape_javascript generate_html(form_builder, method, options)
  end
end
